package com.lxm.config.RabbitMqQueue;

import com.lxm.common.contants.BlogConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DelayBlogQueueConfig {


    //声明订单支付交换机
    @Bean(BlogConstants.BLOG_EXCHANGE_NAME)
    public Exchange orderPayExchange() {
        return ExchangeBuilder
                .topicExchange(BlogConstants.BLOG_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    //声明订单支付队列
    @Bean(BlogConstants.BLOG_QUEUE_NAME)
    public Queue orderPayQueue() {
        return QueueBuilder
                .durable(BlogConstants.BLOG_QUEUE_NAME)
                //为支付队列绑定对应的死信交换机
                .deadLetterExchange(BlogConstants.BLOG_DEAD_EXCHANGE_NAME)
                //为支付队列设置死信路由key，这个是订单支付队列中死信发送到死信队列的路由key
                .deadLetterRoutingKey(BlogConstants.BLOG_DEAD_LETTER_ROUTING_KEY_NAME)
                .build();
    }

    //绑定,将订单支付队列和订单交换机绑定
    @Bean
    public Binding orderPayBinding(@Qualifier(BlogConstants.BLOG_QUEUE_NAME) Queue blogQueue,
                                   @Qualifier(BlogConstants.BLOG_EXCHANGE_NAME)
                                   Exchange blogExchange) {
        return BindingBuilder
                .bind(blogQueue)
                .to(blogExchange)
                .with(BlogConstants.BLOG_ROUTING_KEY_NAME)
                .noargs();
    }

    //  死信交换机
    @Bean(BlogConstants.BLOG_DEAD_EXCHANGE_NAME)
    public Exchange orderPayDeadExchange() {
        return ExchangeBuilder
                .topicExchange(BlogConstants.BLOG_DEAD_EXCHANGE_NAME)
                .durable(true)
                .build();
    }
    //死信队列

    @Bean(BlogConstants.BLOG_DEAD_QUEUE_DEAD_NAME)
    public Queue orderPayDeadQueue() {
        return QueueBuilder
                .durable(BlogConstants.BLOG_DEAD_QUEUE_DEAD_NAME)
                .build();
    }

    //绑定,将死信队列和死信交换机绑定
    @Bean
    public Binding orderPayDeadBinding(@Qualifier(BlogConstants.BLOG_DEAD_QUEUE_DEAD_NAME) Queue
                                               blogDeadQueue,
                                       @Qualifier(BlogConstants.BLOG_DEAD_EXCHANGE_NAME)
                                       Exchange blogDeadExchange) {
        return BindingBuilder
                .bind(blogDeadQueue)
                .to(blogDeadExchange)
                .with("#")
                .noargs();
    }
}
